package com.yic.module.mes.controller.admin.processRoute;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.base.api.factory.FactoryApi;
import com.yic.module.base.api.factory.dto.FactoryRespDTO;
import com.yic.module.system.api.dept.DeptApi;
import com.yic.module.system.api.dept.dto.DeptRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.mes.controller.admin.processRoute.vo.*;
import com.yic.module.mes.dal.dataobject.processRoute.ProcessRouteDO;
import com.yic.module.mes.convert.processRoute.ProcessRouteConvert;
import com.yic.module.mes.service.processRoute.ProcessRouteService;

@Tag(name = "管理后台 - 工艺路线")
@RestController
@RequestMapping("/mes/process-route")
@Validated
public class ProcessRouteController {

    @Resource
    private ProcessRouteService processRouteService;
    @Resource
    private FactoryApi factoryApi;
    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建工艺路线")
    @PreAuthorize("@ss.hasPermission('mes:process-route:create')")
    public CommonResult<Long> createProcessRoute(@Valid @RequestBody ProcessRouteCreateReqVO createReqVO) {
        return success(processRouteService.createProcessRoute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工艺路线")
    @PreAuthorize("@ss.hasPermission('mes:process-route:update')")
    public CommonResult<Boolean> updateProcessRoute(@Valid @RequestBody ProcessRouteUpdateReqVO updateReqVO) {
        processRouteService.updateProcessRoute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工艺路线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:process-route:delete')")
    public CommonResult<Boolean> deleteProcessRoute(@RequestParam("id") Long id) {
        processRouteService.deleteProcessRoute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工艺路线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:process-route:query')")
    public CommonResult<ProcessRouteRespVO> getProcessRoute(@RequestParam("id") Long id) {
        ProcessRouteDO processRoute = processRouteService.getProcessRoute(id);
        return success(ProcessRouteConvert.INSTANCE.convert(processRoute));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工艺路线分页")
    @PreAuthorize("@ss.hasPermission('mes:process-route:query')")
    public CommonResult<PageResult<ProcessRouteRespVO>> getProcessRoutePage(@Valid ProcessRoutePageReqVO pageVO) {
        PageResult<ProcessRouteDO> pageResult = processRouteService.getProcessRoutePage(pageVO);
        PageResult<ProcessRouteRespVO> resp = ProcessRouteConvert.INSTANCE.convertPage(pageResult);
        resp.getList().forEach(item -> {
            //封装工厂信息
            FactoryRespDTO factory = factoryApi.getFactory(item.getFactoryId());
            if (ObjectUtil.isNotEmpty(factory)) {
                item.setFactoryName(factory.getName());
            }
        });
        return success(resp);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工艺路线 Excel")
    @PreAuthorize("@ss.hasPermission('mes:process-route:export')")
    @OperateLog(type = EXPORT)
    public void exportProcessRouteExcel(@Valid ProcessRouteExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProcessRouteDO> list = processRouteService.getProcessRouteList(exportReqVO);
        // 导出 Excel
        List<ProcessRouteExcelVO> datas = ProcessRouteConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工艺路线.xls", "数据", ProcessRouteExcelVO.class, datas);
    }

}
